﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace Grizzy_X
{
    public partial class textobject : Form
    {
        public textobject()
        {
            InitializeComponent();
        }

        public void NewPO(string CurLayer, int ID, int count)
        {
            this.textBox4.Text = CurLayer;
            this.textBox1.Text = ID.ToString();
            this.richTextBox1.Text = count.ToString();
        }

        public void InitializeField(string fieldName)
        {
            this.dataGridView1.Rows.Add(fieldName);
            this.dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].ReadOnly = true;
        }

        public DataGridView getData()
        {
            return this.dataGridView1;
        }

        public bool isOK = false;
        public bool hasInputError = false;

        private void 确定button_Click(object sender, EventArgs e)
        {
            if (Controller.DBExists == true)
            {
                //将新增对象的属性信息插入到当前图层属性表中
                CText curText = ((CTextLayer)Controller.CurrentLayer).Text_Set[((CTextLayer)Controller.CurrentLayer).Text_Set.Count - 1];
                Persistance.cmd.CommandText = "INSERT INTO " + Controller.CurrentLayer.Name +
                    "(" + "文本对象ID"; 
                List<string> fieldValue = new List<string>();

                //若该图层还未设置属性值
                if (Controller.CurrentLayer.AttriFields.Count == 0)
                {
                    Persistance.cmd.CommandText += ")VALUES(" + curText.GetID() + ");";
                }
                else
                {
                    for (int i = 0; i < this.getData().Rows.Count; i++)
                    {
                        if (Controller.CurrentLayer.AttriFields[i][3].Contains("不允许为空"))
                        {
                            if (this.getData().Rows[i].Cells[1].Value == null)
                            {
                                MessageBox.Show("警告：" + Controller.CurrentLayer.AttriFields[i][0] + "字段不允许为空值");
                                hasInputError = true;
                                this.isOK = false;
                            }
                            else
                            {
                                if (Controller.CurrentLayer.AttriFields[i][1].Contains("Int") || Controller.CurrentLayer.AttriFields[i][1].Contains("Byte") || Controller.CurrentLayer.AttriFields[i][1].Contains("Float"))
                                {
                                    if (IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", this.getData().Rows[i].Cells[1].Value.ToString()) == false)//如果输入非数值型的数值
                                    {
                                        MessageBox.Show("警告：" + Controller.CurrentLayer.AttriFields[i][0] + "的属性值必须为数值型！");
                                        hasInputError = true;
                                        this.isOK = false;
                                    }
                                    else
                                    {
                                        hasInputError = false;
                                        fieldValue.Add(this.getData().Rows[i].Cells[1].Value.ToString());
                                    }
                                }
                                else if (Controller.CurrentLayer.AttriFields[i][1].Contains("Bool"))
                                {
                                    if (this.getData().Rows[i].Cells[1].Value.ToString() != "-1" || this.getData().Rows[i].Cells[1].Value.ToString() != "0")
                                    {
                                        MessageBox.Show("警告：" + Controller.CurrentLayer.AttriFields[i][0] + "必须输入真假值！真：-1；假：0");
                                        hasInputError = true;
                                        this.isOK = false;
                                    }
                                    else
                                    {
                                        hasInputError = false;
                                        fieldValue.Add(this.getData().Rows[i].Cells[1].Value.ToString());
                                    }
                                }
                                else
                                {
                                    hasInputError = false;
                                    fieldValue.Add(this.getData().Rows[i].Cells[1].Value.ToString());
                                }
                            }
                        }

                        else if (this.getData().Rows[i].Cells[1].Value == null)
                            fieldValue.Add("0");
                        else
                            fieldValue.Add(this.getData().Rows[i].Cells[1].Value.ToString());
                    }

                }
                if (hasInputError == false)//
                {
                    if (Controller.CurrentLayer.AttriFields.Count != 0)
                    {
                        CLayer curLayer = Controller.CurrentLayer;
                        Persistance.cmd.CommandText += ",";
                        if (curLayer.AttriFields.Count >= 2)
                        {
                            for (int i = 0; i < curLayer.AttriFields.Count - 1; i++)
                            {
                                Persistance.cmd.CommandText += curLayer.AttriFields[i][0] + ",";
                            }
                        }
                        Persistance.cmd.CommandText += curLayer.AttriFields[curLayer.AttriFields.Count - 1][0] + ")";
                        Persistance.cmd.CommandText += "VALUES(" + curText.GetID() + ",";
                        if (curLayer.AttriFields.Count >= 2)
                        {
                            for (int i = 0; i < fieldValue.Count - 1; i++)
                            {
                                Persistance.cmd.CommandText += "'" + fieldValue[i] + "',";
                            }
                        }
                        Persistance.cmd.CommandText += "'" + fieldValue[fieldValue.Count - 1] + "');";
                    }
                    Persistance.myConnection.Open();
                    int result = Persistance.cmd.ExecuteNonQuery();
                    Persistance.myConnection.Close();
                    //至此，添加新对象的属性结束

                    isOK = true;
                    this.Close();
                }
                //else
                //  this.Close();
            }
        }

        private void 取消button_Click(object sender, EventArgs e)
        {
            isOK = false;
            this.Close();
        }

        private static bool IsRegEx(string regExValue, string itemValue)
        {
            try
            {
                Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
                if (regex.IsMatch(itemValue)) return true;
                else return false;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
            }
        }
    }
}
